package com.renwei.yunlong.fragment;

import android.annotation.SuppressLint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.work.PhotoDisplayActivity;
import com.renwei.yunlong.adapter.WorkAnnexAdapter;
import com.renwei.yunlong.base.BaseLazyFragment;
import com.renwei.yunlong.bean.WorkAnnexBean;
import com.renwei.yunlong.event.ComplainPageRefreshEvent;
import com.renwei.yunlong.event.ProblemPageRefreshEvent;
import com.renwei.yunlong.event.SparePageRefreshEvent;
import com.renwei.yunlong.event.WorkPageRefreshEvent;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.utils.StringUtils;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;
import com.renwei.yunlong.http.Api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * 工单  附件  页面
 * <p>
 * Created by Administrator on 2017/9/6.
 */

public class BaseAnnexFragment extends BaseLazyFragment implements HttpTaskListener {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;

    private WorkAnnexAdapter adapter;
    private String id;
    private String from;
    private String outsourceFlag = "0";

    @SuppressLint("ValidFragment")
    public BaseAnnexFragment(String requestId, String from) {
        this.id = requestId;
        this.from = from;
    }

    public BaseAnnexFragment(String requestId, String from, String outsourceFlag) {
        this.id = requestId;
        this.from = from;
        if (!TextUtils.isEmpty(outsourceFlag)) {
            this.outsourceFlag = outsourceFlag;
        }
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_work_annex;
    }

    @Override
    public void onLazyLoad() {
        initView();
        initData();
    }

    public void initView() {
        EventBus.getDefault().register(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new WorkAnnexAdapter(getActivity());
        recyclerView.setAdapter(adapter);
    }

    /**
     * 当前刷新
     */
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onRefresh(WorkPageRefreshEvent event) {
        initData();
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onRefresh(ProblemPageRefreshEvent event) {
        initData();
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onRefresh(ComplainPageRefreshEvent event) {
        initData();
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onRefresh(SparePageRefreshEvent event) {
        initData();
    }

    public void initData() {
        HashMap<String, String> map = new HashMap<>();
        String type = "";
        if ("work".equals(from)) {
            type = "1";
        } else if ("problem".equals(from)) {
            type = "6";
        } else if ("complain".equals(from)) {
            type = "1";
        } else if ("spare".equals(from)) {
            type = "30";
        } else if ("know".equals(from)) {
            type = "0,7";
        }
        map.put("correlationId", id);
        map.put("type", type);
        ServiceRequestManager.getManager().getWorkAnnex(getContext(), StringUtils.value(outsourceFlag), JsonMapUtil.mapToJson(map), this);
    }

    private void setDataList(final List<WorkAnnexBean.Rows> mList) {
        final ArrayList<String> paths = new ArrayList<>();
        for (WorkAnnexBean.Rows rows : mList) {
            if (TextUtils.isEmpty(rows.getIsInstead())) {
                if ("1".equals(companyType)) {
                    paths.add(Api.$().OWNER_FILE_URL + rows.getAttachPath());
                } else if ("2".equals(companyType)) {
                    paths.add(Api.$().SERVICE_FILE_URL + rows.getAttachPath());
                }
            } else {
                if ("0".equals(rows.getIsInstead())) {
                    paths.add(Api.$().OWNER_FILE_URL + rows.getAttachPath());
                } else if ("1".equals(rows.getIsInstead())) {
                    paths.add(Api.$().$().SERVICE_FILE_URL + rows.getAttachPath());
                }
            }
        }
        adapter.setData(mList);
        adapter.setOnItemClickLitener(new BaseRecyclerViewAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                PhotoDisplayActivity.OpenActivity(getContext(), paths, position);
            }
        });

    }

    @Override
    public void onSuccess(int requestId, String result) {
        WorkAnnexBean ownerBean = new Gson().fromJson(result, WorkAnnexBean.class);
        if (ownerBean.getMessage().getCode() == 200) {
            if (ownerBean.getRows().size() == 0) {
                stateLayout.showEmptyView();
            } else {
                stateLayout.showContentView();
            }
            setDataList(ownerBean.getRows());
        }
    }

    @Override
    public void onException(int requestId, String code) {
        stateLayout.showErrorView();
    }
}
